//
//  YSExperienceOrderDetailCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/8/5.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  体验券订单详情

#import "YSExperienceOrderDetailCtrl.h"
#import "YSAccountCenterCtrl.h"
#import "YSAccountOrderStatusCtrl.h"
#import "YSOrderDetailBottomBar.h"
#import "YSExperienceOrderDetailHeaderView.h"
#import "YSExperienceOrderDetailFooterView.h"
#import "YSMineDataTool.h"
#import "YSOrderCell.h"
#import "YSProductDetailCtrl.h"
#import "YSExperienceOrderDetailFooterHeaderView.h"

@interface YSExperienceOrderDetailCtrl ()<UITableViewDataSource,UITableViewDelegate,YSOrderDetailBottomBarDelegate,YSAccountCenterDelegate,YSAccountOrderStatusDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) YSExperienceOrderDetailHeaderView *headerView;
@property (strong, nonatomic) YSExperienceOrderDetailFooterView *footerView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (strong, nonatomic) YSOrder *order;
@property (strong, nonatomic) YSOrderDetailBottomBar *bottomBar;
@property (strong, nonatomic) NSMutableArray *volumeArr;
@end

@implementation YSExperienceOrderDetailCtrl

-(instancetype)initWithOrder:(YSOrder *)order{
    self = [super init];
    if(self){
        self.order = order;
    }
    return self;
}

-(NSMutableArray *)volumeArr{
    if(!_volumeArr){
        _volumeArr = [NSMutableArray array];
    }
    return _volumeArr;
}

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"订单详情"];
    [self initViews];
    [self requestData];
}

-(void)requestData{
    [YSMineDataTool getOrderDetailWithOrderCode:self.order.orderCode success:^(YSOrderDetailRes *result) {
        if(result.isSucc){
            [self.headerView setOrder:self.order];
            if(result.volumeList.count){
                [self.tableView setTableFooterView:self.footerView];
                self.order.voucherAmount = result.voucherAmount;
               [self.footerView setOrderWithVolume:self.order itemArray:result.volumeList];
            }else{
                YSExperienceOrderDetailFooterHeaderView *footerView = [[YSExperienceOrderDetailFooterHeaderView alloc]init];
                [footerView setPrice:self.order.orderPrice voucherAmount:result.voucherAmount];
                [self.tableView setTableFooterView:footerView];
            }
            [self.itemArray removeAllObjects];
            [self.itemArray addObjectsFromArray:self.order.prodList];
            [self.tableView reloadData];
        }else{
            if(result.code == 1001 || result.code == 1002){
                [Utils clearExitInfo];
                [Utils showLoginCtrl:self];
            }
           ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);
    }];
}

-(void)initViews{
    self.headerView = [YSExperienceOrderDetailHeaderView headerView];
    self.footerView = [[YSExperienceOrderDetailFooterView alloc]init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH-NavWithStatusBarH) style:UITableViewStylePlain];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setTableHeaderView:self.headerView];
    [self.tableView setBackgroundColor:PUBLIC_BG];
    [self.view addSubview:self.tableView];
    
    if(self.order.orderStatus == OrderExperienceVolumeChildNotPayType){
        self.bottomBar = [YSOrderDetailBottomBar bottomBar];
        [self.bottomBar setWidth:SCREENW];
        [self.bottomBar setDelegate:self];
        if(self.order.orderStatus == OrderExperienceVolumeChildNotPayType){
            [self.bottomBar.leftBtn setTitle:@"取消" forState:UIControlStateNormal];
            [self.bottomBar.rightBtn setTitle:@"立即支付" forState:UIControlStateNormal];
        }
        [self.bottomBar setY:SCREENH-self.bottomBar.height-NavWithStatusBarH];
        [self.view addSubview:self.bottomBar];
        self.tableView.height = SCREENH-NavWithStatusBarH-self.bottomBar.height;
    }
}

-(void)bottomBtnClick:(NSInteger)tag{
    if(self.order.orderStatus == OrderExperienceVolumeChildNotPayType){
        if(tag == 0){//取消
            [self cancleOrder:self.order.orderCode];
        }else{//立即支付
            YSAccountCenterCtrl *ctrl =  [[YSAccountCenterCtrl alloc]initWithOrdreCode:self.order.orderCode  selectProdIds:nil targetType:accountCenterFinishOrderDetailType productType:experienceProductType];
            [ctrl setDelegate:self];
            [self.navigationController pushViewController:ctrl animated:YES];
        }
    }
}

-(void)accountCenterFinish:(int)type status:(NSString *)status{
    if(type == accountCenterFinishOrderDetailType){
        [self toOrderStatusPage:status];
    }
}

/**
 *  结算中心页面关闭,回调跳转至支付状态页面
 *
 *  @param resultCode 状态码
 */
-(void)toOrderStatusPage:(NSString *)resultCode
{
    YSAccountOrderStatusCtrl *ctr = [YSAccountOrderStatusCtrl initWithResultCode:resultCode targetType:orderStatusFinishOrderDetailType];
    ctr.delegate = self;
    //进入状态页面
    [self.navigationController pushViewController:ctr  animated:YES];
}

/**
 *  订单状态页面关闭,回调跳转至订单列表页
 *
 *  @param type
 */
-(void)orderStatusFinish:(int)type
{
    if(orderStatusFinishOrderDetailType == type){
        [self.navigationController popViewControllerAnimated:YES];
    }
}

/**
 *  取消订单
 */
-(void)cancleOrder:(NSString *)orderCode{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"取消订单" message:@"确定要取消吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [DefaultsHelper setObject:orderCode forKey:kOrderCode];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *orderCode = [DefaultsHelper stringForKey:kOrderCode];
        [YSMineDataTool cancleOrderWithOrderCode:orderCode success:^(BaseResponse *result) {
            if(result.isSucc){
                [DefaultsHelper setObject:nil forKey:kOrderCode];
                [[NSNotificationCenter defaultCenter]postNotificationName:kSubviewsUpdateOrderStatus object:nil];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                ShowEnterTips(result.data);
            }
        } failure:^(NSError *error) {
            ShowTips(kRequestFailure);
        }];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSOrderCell *cell =  [YSOrderCell cellWithTableView:tableView];
    cell.product = self.itemArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSOrderCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger prodId = [self.itemArray[indexPath.row] prodMainId];
    YSProductDetailCtrl *ctrl =  [[YSProductDetailCtrl alloc]initWithProdId:prodId];
    [self.navigationController pushViewController:ctrl animated:YES];
}

@end
